package com.jprocms.module.cms.service.accesstrend;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jprocms.module.cms.controller.admin.accesstrend.vo.AccessTrendListReqVO;
import com.jprocms.module.cms.dal.dataobject.accessstatistics.AccessStatisticsDO;
import com.jprocms.module.cms.dal.dataobject.accesstrend.AccessTrendDO;
import com.jprocms.module.cms.dal.mysql.accesstrend.AccessTrendMapper;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.OffsetDateTime;
import java.util.List;

import static com.jprocms.module.cms.dal.dataobject.accessstatistics.AccessStatisticsDO.*;


/**
 * 访问统计趋势 Service 实现类
 *
 * @author jprocms
 */
@Service
@Validated
public class AccessTrendServiceImpl extends ServiceImpl<AccessTrendMapper, AccessTrendDO> implements AccessTrendService {

    @Resource
    private AccessTrendMapper accessTrendMapper;

    @Override
    public List<AccessTrendDO> getAccessTrendList(AccessTrendListReqVO exportReqVO) {
        return accessTrendMapper.selectList(exportReqVO);
    }


    @Transactional(rollbackFor = Exception.class)
    public void deleteBeforeDateString(String dateString, short period) {
        accessTrendMapper.deleteBeforeDateString(dateString, period);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteByDateString(String dateString) {
        accessTrendMapper.deleteByDateString(dateString);
    }

    public List<AccessTrendDO> statByMonth(OffsetDateTime begin, OffsetDateTime end) {
        return accessTrendMapper.statByDate(AccessTrendDO.PERIOD_DAY, begin.format(DAY_FORMATTER), end.format(DAY_FORMATTER));
    }

    public List<AccessTrendDO> selectFullList(@Nullable Long siteId, short period, @Nullable String begin, String end) {
        AccessTrendListReqVO reqVO = new AccessTrendListReqVO();
        reqVO.setSiteId(siteId).setStatPeriod(period).setDateBeginString(begin).setDateEndString(end);
        switch (period) {
            case AccessTrendDO.PERIOD_MONTH:
                return AccessStatisticsDO.fillEmptyDate(getAccessTrendList(reqVO), begin, end, MONTH_FORMATTER, MONTH_DISPLAY_FORMATTER,
                        date -> date.plusMonths(1), AccessTrendDO::new);
            case AccessTrendDO.PERIOD_DAY:
                return AccessStatisticsDO.fillEmptyDate(getAccessTrendList(reqVO), begin, end, DAY_FORMATTER, DAY_DISPLAY_FORMATTER,
                        date -> date.plusDays(1), AccessTrendDO::new);
            case AccessTrendDO.PERIOD_HOUR:
                return AccessStatisticsDO.fillEmptyDate(getAccessTrendList(reqVO), begin, end, HOUR_FORMATTER, HOUR_DISPLAY_FORMATTER,
                        date -> date.plusHours(1), AccessTrendDO::new);
            case AccessTrendDO.PERIOD_MINUTE:
                return AccessStatisticsDO.fillEmptyDate(getAccessTrendList(reqVO), begin, end, MINUTE_FORMATTER, MINUTE_DISPLAY_FORMATTER,
                        date -> date.plusMinutes(1), AccessTrendDO::new);
            default:
                throw new IllegalArgumentException("period not supported: " + period);
        }
    }

    //TODO 站点删除 流量统计数据需要删除

}
